import { Base64 } from 'js-base64'

function encodeText(text: string, encoding: string): string {
  if (encoding === 'unicode') {
    const buf = new ArrayBuffer(text.length * 4)
    const bufView = new Uint16Array(buf)
    for (let i = 0, strlen = text.length; i < strlen; i++) {
      bufView[i] = text.charCodeAt(i)
    }
    let binary = ''
    const bytes = new Uint8Array(buf)
    const len = bytes.byteLength
    for (let i = 0; i < len; i++) {
      binary += String.fromCharCode(bytes[i])
    }
    return window.btoa(binary)
  }
  else {
    return Base64.encode(text)
  }
}

export { encodeText }
